<?php
/**
 * 控制器抽象类
 *
 * @author river
 *
 */
abstract class Gospel_Controller_Abstract {
	/**
	 * request
	 *
	 * @var Gospel_Http_Request_Abstract
	 */
	protected $_req;

	/**
	 * constructor
	 *
	 * @param Gospel_Http_Request $request
	 * @throws Gospel_Controller_Exception
	 */
	public function __construct(Gospel_Http_Request_Abstract &$request){
		try{
			$this->_req = $request;
			$this->init();
			$this->beforeDespatch($this->_req);
			$this->dispathAction($this->_req);
			$this->afterDispatch($this->_req);
		}catch (Gospel_Controller_Exception $e){
			throw $e;
		}
	}

	/**
	 * 分发Action
	 *
	 * @return void
	 * @throws Gospel_Controller_Exception
	 */
	protected function dispathAction(Gospel_Http_Request_Abstract &$request){
		$action = $request->getAction();
		$action = strtolower($action);
		$action .= 'Action';
		if(method_exists($this, $action)){
			$this->{$action}();
		}else{
			throw new Gospel_Controller_Exception('not found the '.$action.' in '.get_class($this), 404);
		}
	}

	/**
	 * 分发action之前
	 *
	 * @return void
	 */
	protected function beforeDespatch(Gospel_Http_Request_Abstract &$request){

	}

	/**
	 * 分发action之后
	 *
	 * @return void
	 */
	protected function afterDispatch(Gospel_Http_Request_Abstract &$request){

	}

	/**
	 * 获取请求
	 *
	 * @return Gospel_Http_Request
	 */
	protected function getRequest(){
		return $this->_req;
	}

	/**
	 * 跳转action
	 *
	 * @param string $url
	 * @param array $para
	 * @return void
	 */
	protected function _redirect($url, array $para = array()){

	}

	/**
	 * 重写初始化
	 *
	 * @return void
	 */
	protected function init(){

	}
}
//end